package com.icongyou.enterprise.talent_pool.service.impl;

import com.icongyou.enterprise.data_aggregation.entity.AssignmentEntity;
import com.icongyou.enterprise.data_aggregation.service.DataAggregationService;
import com.icongyou.enterprise.talent_pool.entity.AssignmentTagEntity;
import com.icongyou.enterprise.talent_pool.mapper.AssignmentTagMapper;
import com.icongyou.enterprise.talent_pool.service.AIModelService;
import com.icongyou.enterprise.talent_pool.service.AssignmentWeightService;
import com.icongyou.enterprise.talent_pool.service.TagAnalysisResult;
import com.icongyou.enterprise.talent_pool.service.TagProcessingService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class AssignmentTagServiceImplTest {

    @Mock
    private AssignmentTagMapper assignmentTagMapper;

    @Mock
    private TagProcessingService tagProcessingService;

    @Mock
    private AIModelService aiModelService;

    @Mock
    private AssignmentWeightService weightService;

    @Mock
    private DataAggregationService dataAggregationService;

    @InjectMocks
    private AssignmentTagServiceImpl assignmentTagService;

    private String assignmentId;
    private AssignmentEntity assignmentEntity;
    private TagAnalysisResult tagAnalysisResult;

    @BeforeEach
    void setUp() {
        assignmentId = "test-assignment-123";

        assignmentEntity = new AssignmentEntity();
        assignmentEntity.setDescription("Test assignment description");
        assignmentEntity.setAssignmentName("Test Assignment");

        tagAnalysisResult = new TagAnalysisResult();
        tagAnalysisResult.setTagName("Java");
        tagAnalysisResult.setRelevance(0.85);
        tagAnalysisResult.setSimilarTags(Arrays.asList("Java编程", "Java开发"));
    }

    @Test
    void calculateAssignmentTagAssociations_WithNoAssignments_ShouldDoNothing() {
        // Arrange
        when(assignmentTagMapper.getAssignmentsToCalculate(any(LocalDateTime.class)))
                .thenReturn(Collections.emptyList());

        // Act
        assignmentTagService.calculateAssignmentTagAssociations();

        // Assert
        verify(assignmentTagMapper, never()).deleteByAssignmentIds(anyList());
        verify(assignmentTagMapper, never()).batchInsertAssignmentTags(anyList());
    }

    @Test
    void calculateAssignmentTagAssociations_WithAssignments_ShouldProcessSuccessfully() {
        // Arrange
        List<String> assignmentIds = Arrays.asList("assign-1", "assign-2");
        when(assignmentTagMapper.getAssignmentsToCalculate(any(LocalDateTime.class)))
                .thenReturn(assignmentIds);

        when(dataAggregationService.getAssignmentInfo(anyString())).thenReturn(assignmentEntity);
        when(aiModelService.analyzeAssignmentTags(anyString())).thenReturn(Arrays.asList(tagAnalysisResult));
        when(weightService.calculateAssignmentImportanceWeight(anyString())).thenReturn(0.75);
        when(tagProcessingService.processGeneratedTag(anyString(), anyList())).thenReturn("tag-123");

        // Act
        assignmentTagService.calculateAssignmentTagAssociations();

        // Assert
        verify(assignmentTagMapper, atLeastOnce()).deleteByAssignmentIds(anyList());
        verify(assignmentTagMapper, atLeastOnce()).batchInsertAssignmentTags(anyList());
    }

    @Test
    void processSingleAssignment_WithValidDescription_ShouldReturnTags() {
        // Arrange
        when(dataAggregationService.getAssignmentInfo(assignmentId)).thenReturn(assignmentEntity);
        when(aiModelService.analyzeAssignmentTags(anyString())).thenReturn(Arrays.asList(tagAnalysisResult));
        when(weightService.calculateAssignmentImportanceWeight(assignmentId)).thenReturn(0.75);
        when(tagProcessingService.processGeneratedTag(eq("Java"), anyList())).thenReturn("tag-123");

        // Act
        List<AssignmentTagEntity> result = assignmentTagService.processSingleAssignment(assignmentId);

        // Assert
        assertNotNull(result);
        assertFalse(result.isEmpty());
        assertEquals(1, result.size());

        AssignmentTagEntity assignmentTag = result.get(0);
        assertEquals(assignmentId, assignmentTag.getAssignmentId());
        assertEquals("tag-123", assignmentTag.getTagId());
        assertEquals(0.85, assignmentTag.getRelevance());
        assertEquals(0.75, assignmentTag.getAssignmentWeight());
    }

    @Test
    void processSingleAssignment_WithNullDescription_ShouldReturnEmptyList() {
        // Arrange
        assignmentEntity.setDescription(null);
        when(dataAggregationService.getAssignmentInfo(assignmentId)).thenReturn(assignmentEntity);

        // Act
        List<AssignmentTagEntity> result = assignmentTagService.processSingleAssignment(assignmentId);

        // Assert
        assertNotNull(result);
        assertTrue(result.isEmpty());
    }

    @Test
    void processSingleAssignment_WithEmptyDescription_ShouldReturnEmptyList() {
        // Arrange
        assignmentEntity.setDescription("");
        when(dataAggregationService.getAssignmentInfo(assignmentId)).thenReturn(assignmentEntity);

        // Act
        List<AssignmentTagEntity> result = assignmentTagService.processSingleAssignment(assignmentId);

        // Assert
        assertNotNull(result);
        assertTrue(result.isEmpty());
    }

    @Test
    void getAssignmentDescription_WithValidAssignment_ShouldReturnDescription() {
        // Arrange
        when(dataAggregationService.getAssignmentInfo(assignmentId)).thenReturn(assignmentEntity);

        // Act
        String result = assignmentTagService.getAssignmentDescription(assignmentId);

        // Assert
        assertEquals("Test assignment description", result);
    }

    @Test
    void getAssignmentDescription_WithNullAssignment_ShouldReturnNull() {
        // Arrange
        when(dataAggregationService.getAssignmentInfo(assignmentId)).thenReturn(null);

        // Act
        String result = assignmentTagService.getAssignmentDescription(assignmentId);

        // Assert
        assertNull(result);
    }

    @Test
    void getAssignmentDescription_WithEmptyDescription_ShouldReturnAssignmentName() {
        // Arrange
        assignmentEntity.setDescription("");
        when(dataAggregationService.getAssignmentInfo(assignmentId)).thenReturn(assignmentEntity);

        // Act
        String result = assignmentTagService.getAssignmentDescription(assignmentId);

        // Assert
        assertEquals("Test Assignment", result);
    }
}